<template>
  <!-- 厂商管理 -->
  <div class="filterContent">
    <a-form :model="filterForm" layout="inline">
      <a-form-item label="厂商名称">
        <a-input
          v-model:value="filterForm.name"
          class="formInput"
          allow-clear
          placeholder="请输入"
        />
      </a-form-item>
      <a-form-item label="厂商简称">
        <a-input
          v-model:value="filterForm.shortName"
          class="formInput"
          allow-clear
          placeholder="请输入"
        />
      </a-form-item>
      <a-form-item label="厂商编码">
        <a-input
          v-model:value="filterForm.code"
          class="formInput"
          allow-clear
          placeholder="请输入"
        />
      </a-form-item>
      <a-button type="primary" class="filterBtn" @click="handleFilter"
        >查询</a-button
      >
      <a-button type="primary" class="filterBtn greenBtn" @click="handleCompany"
        >新增</a-button
      >
    </a-form>
  </div>
  <div class="tableContent" ref="tableContainerRef">
    <a-table
      :dataSource="tableData"
      :columns="columns"
      size="small"
      :scroll="tableScroll"
      @resizeColumn="handleResizeColumn"
      :pagination="false"
      :loading="tableLoading"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'handle'">
          <a-button type="link" @click="handleEdit(record)">编辑</a-button>
          <a-popconfirm
            ok-text="确认"
            cancel-text="取消"
            @confirm="confirmDel(record)"
          >
            <template #title>
              是否确定删除【{{ record.name }}】的厂商记录?
            </template>
            <a-button type="link" danger>删除</a-button>
          </a-popconfirm>
        </template>
      </template>
    </a-table>
    <a-pagination
      v-model:current="filterForm.current"
      v-model:page-size="filterForm.size"
      :total="total"
      position="bottomRight"
      show-size-changer
      @change="getSupplierPage"
      :show-total="(total) => `总共 ${total} 条`"
    >
      <template #buildOptionText="props">
        <span>{{ props.value }}条/页</span>
      </template>
    </a-pagination>
  </div>
  <!-- 新增厂商 -->
  <AddCompany
    :addCompanyVisible="addCompanyVisible"
    :rowData="rowData"
    :isEdit="isEdit"
    @update:addCompanyVisible="(val) => (addCompanyVisible = val)"
    @getSupplierPage="getSupplierPage"
  />
</template>
<script setup>
// 
import useTableScroll from "@/hooks/useTableScroll"
import AddCompany from "./conpoments/addCompany.vue"

const $api = inject("$api")
const filterForm = ref({
  name: undefined,
  shortName: undefined,
  code: undefined,
  size: 10,
  current: 1,
})
const total = ref()
const addCompanyVisible = ref(false)
const tableData = ref([])
const isEdit = ref(false)
const rowData = ref({})
const tableLoading = ref(false)
const columns = ref([
  {
    title: "厂商名称",
    dataIndex: "name",
    key: "name",
    width: 200,
    resizable: true,
    align: "center",
  },
  {
    title: "厂商简称",
    dataIndex: "shortName",
    key: "shortName",
    width: 200,
    resizable: true,
    align: "center",
  },
  {
    title: "厂商编码",
    dataIndex: "code",
    key: "code",
    width: 150,
    resizable: true,
    align: "center",
  },
  {
    title: "创建人",
    dataIndex: "createdUserName",
    key: "createdUserName",
    width: 120,
    resizable: true,
    align: "center",
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
    key: "createTime",
    width: 180,
    resizable: true,
    align: "center",
  },
  {
    title: "备注",
    dataIndex: "remark",
    key: "remark",
    width: 160,
    resizable: true,
    align: "center",
  },
  {
    title: "更新人",
    dataIndex: "updatedUserName",
    key: "updatedUserName",
    width: 120,
    resizable: true,
    align: "center",
  },
  {
    title: "更新时间",
    dataIndex: "updateTime",
    key: "updateTime",
    width: 180,
    resizable: true,
    align: "center",
  },
  {
    title: "操作",
    dataIndex: "handle",
    key: "handle",
    //width: 120,
    align: "center",
    fixed: "right",
  },
])
function handleResizeColumn(w, col) {
  col.width = w
}
const handleCompany = () => {
  isEdit.value = false
  addCompanyVisible.value = true
}
const confirmDel = async (data) => {
  const res = await $api.supplierDel(data.id)
  if (res && res.code === 0) {
    message.success("删除成功")
    getSupplierPage()
  }
}
const getSupplierPage = async () => {
  const params = {
    ...filterForm.value,
  }
  tableLoading.value = true
  const res = await $api.supplierPage(params)
  tableLoading.value = false
  if (res && res.code === 0) {
    tableData.value = res.data.records
    total.value = res.data.total
  }
}
const handleEdit = async (data) => {
  rowData.value = data
  isEdit.value = true
  await nextTick()
  addCompanyVisible.value = true
}
const tableContainerRef = ref(null)
const handleFilter = () => {
  filterForm.value.current = 1
  filterForm.value.size = 10
  getSupplierPage()
}
onMounted(async () => {
  await getSupplierPage()
})

const { tableScroll } = useTableScroll(tableData, tableContainerRef)
</script>

<style lang="less" scoped>
.ant-table {
  .ant-btn {
    padding: 4px 5px;
  }
}
.formInput {
  width: 155px;
}
.filterBtn {
  margin: 0 20px 20px 0;
  &:last-child {
    margin-right: 0;
  }
}
.tableContent {
  border-radius: 4px;
  background-color: #141414;
  padding: 8px;
}
// ::v-deep(.ant-table-scroll-horizontal) {
//   height: calc(100vh - 270px);
//   overflow: auto scroll;
// }
</style>
